﻿using System;
using FireStarterModeller.UI.Dialogs;
using Habanero.Base.Exceptions;
using NUnit.Framework;

namespace FireStarterModeller.Test.UI.Dialogs
{
    [TestFixture]
    public class TestFileExtensionFilterElement : TestBase
    {

        [Test]
        public void Test_Construct()
        {
            //---------------Set up test pack-------------------
            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            FileExtensionFilterElement element = new FileExtensionFilterElement();
            //---------------Test Result -----------------------
            Assert.IsNotNull(element);
            Assert.IsInstanceOf<FileExtensionFilterElement>(element);
        }


        [Test]
        public void Test_Constuct_ShouldHaveEmptyProps()
        {
            //---------------Set up test pack-------------------
            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            FileExtensionFilterElement element = CreateFileExtensionFilterElement();

            //---------------Test Result -----------------------
            Assert.IsNullOrEmpty(element.Extension);
            Assert.IsNullOrEmpty(element.Description);
            Assert.IsNullOrEmpty(element.ExtensionFilter);
        }

        [Test]
        public void Test_IsValid_WhenExtensionAndDescriptionAreEmpty_ShouldReturnFalse()
        {
            //---------------Set up test pack-------------------
            FileExtensionFilterElement element = CreateFileExtensionFilterElement();
            //---------------Assert Precondition----------------
            Assert.IsNullOrEmpty(element.Extension);
            Assert.IsNullOrEmpty(element.Description);
            Assert.IsNullOrEmpty(element.ExtensionFilter);

            //---------------Execute Test ----------------------
            bool isValid = element.IsValid();
            //---------------Test Result -----------------------
            Assert.IsFalse(isValid);
        }

        [Test]
        public void Test_IsValid_WhenExtensionIsEmpty_ShouldReturnFalse()
        {
            //---------------Set up test pack-------------------
            FileExtensionFilterElement element = CreateFileExtensionFilterElement();
            element.Description = GetRandomString();
            //---------------Assert Precondition----------------
            Assert.IsNullOrEmpty(element.Extension);
            Assert.IsNotEmpty(element.Description);
            Assert.IsNullOrEmpty(element.ExtensionFilter);

            //---------------Execute Test ----------------------
            bool isValid = element.IsValid();
            //---------------Test Result -----------------------
            Assert.IsFalse(isValid);
        }

        [Test]
        public void Test_IsValid_WhenDescriptionIsEmpty_ShouldReturnFalse()
        {
            //---------------Set up test pack-------------------
            FileExtensionFilterElement element = CreateFileExtensionFilterElement();
            element.Extension = GetRandomString();
            //---------------Assert Precondition----------------
            Assert.IsNotEmpty(element.Extension);
            Assert.IsNullOrEmpty(element.Description);
            Assert.IsNullOrEmpty(element.ExtensionFilter);

            //---------------Execute Test ----------------------
            bool isValid = element.IsValid();
            //---------------Test Result -----------------------
            Assert.IsFalse(isValid);
        }

        [Test]
        public void Test_IsValid_WhenExtensionAndDescriptionAreNotEmpty_ShouldReturnTrue()
        {
            //---------------Set up test pack-------------------
            FileExtensionFilterElement element = CreateFileExtensionFilterElement();
            element.Extension = GetRandomString();
            element.Description = GetRandomString();
            //---------------Assert Precondition----------------
            Assert.IsNotEmpty(element.Extension);
            Assert.IsNotEmpty(element.Description);
            Assert.IsNullOrEmpty(element.ExtensionFilter);

            //---------------Execute Test ----------------------
            bool isValid = element.IsValid();
            //---------------Test Result -----------------------
            Assert.IsTrue(isValid);
        }

        [Test]
        public void Test_Format_WhenExtensionIsEmpty_ShouldThrowException()
        {
            //---------------Set up test pack-------------------
            FileExtensionFilterElement element = CreateFileExtensionFilterElement();
            element.Description = GetRandomString();
            //---------------Assert Precondition----------------
            Assert.IsNullOrEmpty(element.Extension);
            Assert.IsNotEmpty(element.Description);
            Assert.IsNullOrEmpty(element.ExtensionFilter);

            try
            {

                //---------------Execute Test ----------------------
                element.Format();
                Assert.Fail("Expected to throw an EXCEPTION");
            }
            catch (Exception ex)
            {
                //---------------Test Result -----------------------
                Assert.IsNullOrEmpty(element.ExtensionFilter);
                Assert.IsInstanceOf<HabaneroDeveloperException>(ex);
                StringAssert.Contains("The filter is not valid.  Extension and Description are required. e.g. Extension=mdb, Description=Microsoft Access", ex.Message);
            }
        }


        [Test]
        public void Test_Format_WhenDescriptionIsEmpty_ShouldThrowException()
        {
            //---------------Set up test pack-------------------
            FileExtensionFilterElement element = CreateFileExtensionFilterElement();
            element.Extension = GetRandomString();
            //---------------Assert Precondition----------------
            Assert.IsNotEmpty(element.Extension);
            Assert.IsNullOrEmpty(element.Description);
            Assert.IsNullOrEmpty(element.ExtensionFilter);

            try
            {

                //---------------Execute Test ----------------------
                element.Format();
                Assert.Fail("Expected to throw an EXCEPTION");
            }
            catch (Exception ex)
            {
                //---------------Test Result -----------------------
                Assert.IsNullOrEmpty(element.ExtensionFilter);
                Assert.IsInstanceOf<HabaneroDeveloperException>(ex);
                StringAssert.Contains("The filter is not valid.  Extension and Description are required. e.g. Extension=mdb, Description=Microsoft Access", ex.Message);
            }
        }


        [Test]
        public void Test_Format_WhenExtensionAndDescriptionAreEmpty_ShouldThrowException()
        {
            //---------------Set up test pack-------------------
            FileExtensionFilterElement element = CreateFileExtensionFilterElement();
            //---------------Assert Precondition----------------
            Assert.IsNullOrEmpty(element.Extension);
            Assert.IsNullOrEmpty(element.Description);
            Assert.IsNullOrEmpty(element.ExtensionFilter);

            try
            {

                //---------------Execute Test ----------------------
                element.Format();
                Assert.Fail("Expected to throw an EXCEPTION");
            } 
            catch (Exception ex)
            {
                //---------------Test Result -----------------------
                Assert.IsNullOrEmpty(element.ExtensionFilter);
                Assert.IsInstanceOf<HabaneroDeveloperException>(ex);
                StringAssert.Contains("The filter is not valid.  Extension and Description are required. e.g. Extension=mdb, Description=Microsoft Access", ex.Message);
            }
        }

        [Test]
        public void Test_Format_WhenExtensionAndDescriptionAreSet_ReturnsExtensionFilter()
        {
            //---------------Set up test pack-------------------
            FileExtensionFilterElement element = CreateFileExtensionFilterElement();
            element.Extension = "mdb";
            element.Description = "Microsoft Access";
            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            element.Format();
            //---------------Test Result -----------------------
            Assert.AreEqual("Microsoft Access (*.mdb)|*.mdb", element.ExtensionFilter);
        }


        private static FileExtensionFilterElement CreateFileExtensionFilterElement()
        {
            return new FileExtensionFilterElement();
        }

        private static string GetRandomString()
        {
            return TestUtilsShared.GetRandomString();
        }

    }
}